﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;

public partial class Login : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();


    protected void Page_Load(object sender, EventArgs e)
    {
        //Page.ClientScript.RegisterStartupScript(GetType(), "MyKey", "callAlert();", true);

    }
    protected void OnLogin(object sender, EventArgs e)
    {
        IDataRepository _dataObject = new DataRepository();
        Merchant merchant = new Merchant();
        merchant = new Merchant(_dataObject);
        merchant.Email = txtUserName.Text;
        merchant.Password = txtPassword.Text;
        merchant.IsAdmin = true;
        string[] loginDetail = new string[2];
        loginDetail = merchant.IsValidLogin();
        if (loginDetail[1] != "0")
        {

            merchant.UpdateLoginInfo();
            Response.Redirect("~/default.aspx");


        }
        else
        {
            divMessage.InnerHtml = "<strong>Error!</strong> Invalid Username or password.";
            divMessage.Attributes.Add("class", "alert alert-danger");
            return;
        }


    }

    [System.Web.Services.WebMethod]
    public static string[] checkMail(string emailid, string password)
    {
        //loginDetail[0] means it returns user is expired or not if it is expire then it returns true else it returns false 
        //liginDetails[1] returns Merchant id if it is exits in syatem...
        string s = emailid;
        IDataRepository _dataObject = new DataRepository();
        Merchant merchant = new Merchant();
        merchant = new Merchant(_dataObject);
        merchant.Email = emailid;
        merchant.Password = password;
        merchant.IsAdmin = false;
        string[] loginDetail = new string[2];
        loginDetail = merchant.IsValidLogin();

        if (loginDetail[1] != "0")
        {
            merchant.Id = Convert.ToInt32(loginDetail[1]);
            merchant.GetMerchantById();
            SessionWrapper.Merchant = merchant;
        }
        return loginDetail;
    }
    protected void OnUpgrade(object sender, EventArgs e)
    {
        Response.Redirect("~/MerchantSubscription.aspx");
    }
}